package ieco.domaine;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;

@Entity
@Table(schema = "OPTIONS", name = "CHOIX", uniqueConstraints = { @UniqueConstraint(columnNames = {
		"etudiant_id", "option_id", "annee" }) })
// @SequenceGenerator(schema = "OPTIONS", name = "SEQ_CHOIX", sequenceName =
// "CHOIX_ID_SEQ")
@SuppressWarnings("serial")
public class Choix implements Serializable, Comparable<Choix> {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	@ManyToOne
	@JoinColumn(name = "ETUDIANT_ID", nullable = false)
	private Etudiant etudiant;

	@ManyToOne
	@JoinColumn(name = "OPTION_ID", nullable = false)
	private Option option;

	@NotNull
	@Column(name = "annee")
	private int annee;

	@NotNull
	@Column(name = "preference")
	private int preference;

	@NotNull
	@Column(name = "gagnant")
	private boolean gagnant;

	// Constructeur vide
	public Choix() {
	}

	// Constructeur reprenant les champs not nulls
	public Choix(Etudiant etudiant, Option option, int annee, int preference,
			boolean gagnant) {
		super();
		this.etudiant = etudiant;
		this.option = option;
		this.annee = annee;
		this.preference = preference;
		this.gagnant = gagnant;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Etudiant getEtudiant() {
		return etudiant;
	}

	public void setEtudiant(Etudiant etudiant) {
		this.etudiant = etudiant;
	}

	public Option getOption() {
		return option;
	}

	public void setOption(Option option) {
		this.option = option;
	}

	public int getAnnee() {
		return annee;
	}

	public void setAnnee(int annee) {
		this.annee = annee;
	}

	public int getPreference() {
		return preference;
	}

	public void setPreference(int preference) {
		this.preference = preference;
	}

	public boolean isGagnant() {
		return gagnant;
	}

	public void setGagnant(boolean gagnant) {
		this.gagnant = gagnant;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((etudiant == null) ? 0 : etudiant.hashCode());
		result = prime * result + ((option == null) ? 0 : option.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Choix other = (Choix) obj;
		if (etudiant == null) {
			if (other.etudiant != null)
				return false;
		} else if (!etudiant.equals(other.etudiant))
			return false;
		if (option == null) {
			if (other.option != null)
				return false;
		} else if (!option.equals(other.option))
			return false;
		return true;
	}

	@Override
	public int compareTo(Choix o) {
		return this.getPreference()-o.getPreference();
	}

}
